var qx_num = 0;//展开表格的个数
var qx = {
    //当前打开的行
    expandIndex:-1,
    //新增状态
    formStatus:false,
    //判断按钮显示
    setBtnStatus:function () {
        var row = $('#qx_grid').datagrid('getSelected');
        if (row && row.id) {
            $("#qx_del").show();
        } else {
            $("#qx_del").hide();
        }
    },
    //重置状态
    resetStatus:function () {
        qx.setBtnStatus();
        qx.expandIndex = -1;
        qx.formStatus = false;
    }
}

$(function () {
    //表格初始加载
    $('#qx_grid').datagrid({
        view:detailview,
        detailFormatter:function () {
            return '<div class="detail"></div>';
        },
        onExpandRow:function (index, row) {
            $("#layout_qx").mask();
            //关闭展开的行
            if (qx.expandIndex != -1) {
                $('#qx_grid').datagrid("collapseRow", qx.expandIndex);
            }
            //记录当前展开哪行
            qx.expandIndex = index;
            show_qx_form(index, row);//加载表格面板
        },
        onCollapseRow:function () {
            //删除当前记录的行
            qx.expandIndex = -1;
        },
        onLoadSuccess:qx.setBtnStatus,
        onClickRow:qx.setBtnStatus,
        onSelect:qx.setBtnStatus,
        onDblClickCell:function (index, field) {
            //双击事件
            if (field == "mc") {
                $('#qx_grid').datagrid("expandRow", index);
            }
        }
    });
    //*************按钮触发事件***********
//新增触发
    $('#qx_add').click(
        qx_newItem
    )
//删除触发
    $('#qx_del').click(
        qx_destroyItem
    )
//搜索触发
    $('#qx_search').click(
        qx_search
    )
});

//添加权限行记录 表单
function qx_newItem() {
    //关闭打开的行
    if (qx.expandIndex != -1) {
        $('#qx_grid').datagrid("collapseRow", qx.expandIndex);
    }

    //已有新增判断
    if (!qx.formStatus) {
        $('#qx_grid').datagrid('insertRow', {index:0, row:{isNewRecord:true}});
        qx.formStatus = true;
    }
    $('#qx_grid').datagrid('expandRow', 0);
    $('#qx_grid').datagrid('selectRow', 0);
}

//保存权限
function qx_saveItem(index) {
    $("#layout_qx").mask();
    $('#qx_grid').datagrid('getRowDetail', index).find('form').form('submit', {
        url:$.webPath() + '/sys/qx/add_qx',
        onSubmit:function () {
            var isValid = $(this).form('validate');
            if (!isValid) {
                //$.messager.progress('close');
                $("#layout_qx").mask('hide');
                return false;
            }
            return isValid;
        },
        success:function (data) {
            $("#layout_qx").mask("hide");
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $('#qx_grid').datagrid('reload');
                qx.resetStatus();
                $.messager.show({
                    title:'提示',
                    msg:obj.message,
                    timeout:3000,
                    showType:'slide'
                });
            } else{
                $.messager.alert('提示', obj.message, 'error');
            }
        }
    });
}
//取消权限表单
function qx_cancelItem(index) {
    var row = $('#qx_grid').datagrid('getRows')[index];
    if (row.isNewRecord) {
        $('#qx_grid').datagrid('deleteRow', index);
    } else {
        $('#qx_grid').datagrid('collapseRow', index);
    }
    $('#qx_grid').datagrid('reload');
    qx.resetStatus();
}
//删除权限
function qx_destroyItem() {
    var row = $('#qx_grid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除此权限？', function (r) {
            if (r) {
                var index = $('#qx_grid').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/sys/qx/delete_qx",
                    dataType: 'json',
                    type: 'POST',
                    data: {qxId:row.id},
                    beforeSend: function () {
                        $("#layout_qx").mask();
                    },
                    success:function(data){
                        $("#layout_qx").mask("hide");
                        if(data.statusCode == 200){
                          //  $('#qx_grid').datagrid('deleteRow', index);
                            $('#qx_grid').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                            qx.resetStatus();
                        } else{
                            $.messager.alert('提示', data.message, 'error');
                        }
                    }
                });
//                $.post($.webPath() + '/sys/qx/delete_qx', {qxId:row.id}, function (data) {
//                    var obj = eval('(' + data + ')');
//                    if (obj.statusCode == 200){
//                        $('#qx_grid').datagrid('deleteRow', index);
//                        $.messager.show({
//                            title:'提示',
//                            msg:obj.message,
//                            timeout:3000,
//                            showType:'slide'
//                        });
//                        qx.resetStatus();
//                    }else{
//                        $.messager.alert('提示', obj.message, 'error');
//                    }
//                });
            }
        });
    } else {
        $.messager.alert('提示', '请选择要删除的权限！', 'warning');
    }
}

//查询函数
function qx_search() {
    $('#qx_grid').datagrid('load', {
        mc:$('#qx_mc').val()
    });
}
//展开权限表单
function show_qx_form(index, row) {
    $("#layout_qx").mask("hide");
    var ddv = $("#qx_grid").datagrid('getRowDetail', index).find('div.detail');
    var url = $.webPath() + '/sys/qx/qx_content?index=' + index;
    if (row.id)
        url += '&qxId=' + row.id;
    ddv.panel({
        border:false,
        cache:true,
        href:url,
        onLoad:function () {
            $('#qx_grid').datagrid('fixDetailRowHeight', index);
            $('#qx_grid').datagrid('selectRow', index);

            $('#qx_grid').datagrid('getRowDetail', index).find('input[comboname="qx.cd.id"]').combotree({
                onChange:function (newValue, oldValue) {
                    $('#qx_grid').datagrid('getRowDetail', index).find('input[comboname="qx.gnczList.id"]').combobox('clear');
                    $('#qx_grid').datagrid('getRowDetail', index).find('input[comboname="qx.ymysList.id"]').combobox('clear');
                    $('#qx_grid').datagrid('getRowDetail', index).find('input[comboname="qx.gnczList.id"]').combobox('reload', $.webPath() + '/sys/cd/get_gncz?cdId=' + newValue);
                    $('#qx_grid').datagrid('getRowDetail', index).find('input[comboname="qx.ymysList.id"]').combobox('reload', $.webPath() + '/sys/cd/get_ymys?cdId=' + newValue);
                }
            })

            if (row.cd_id != null)
                $('#qx_grid').datagrid('getRowDetail', index).find('input[comboname="qx.cd.id"]').combotree('setValues', row.cd_id.split(","));
            if (row.gnczList_id != null)
                $('#qx_grid').datagrid('getRowDetail', index).find('input[comboname="qx.gnczList.id"]').combobox('setValues', row.gnczList_id.split(","));
            if (row.ymysList_id != null)
                $('#qx_grid').datagrid('getRowDetail', index).find('input[comboname="qx.ymysList.id"]').combobox('setValues', row.ymysList_id.split(","));

            qx.setBtnStatus();
        }

    });
    $('#qx_grid').datagrid('fixDetailRowHeight', index);
    qx_num++;
}